﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Settings.aspx.cs" Inherits="VisualAgile.Web.Views.Project.Settings" %>

<mvc:ExtScriptManager ID="ExtScriptManager" runat="server" />  
<mvc:ExtJSON ID="settingCfgs" runat="server">
        <xtype>
            <mvc:ExtItem modal="true" layout="border" width="600" title="Settings" height="500">
                <items>
                    <mvc:ExtItem xtype="tabpanel" deferredRender="false" region="center" enableTabScroll="true"
                        anchor="100% 0" activeTab="0" frame="false" border="false">
                        <items>
                           <mvc:ExtItem xtype="panel" title="Project" frame="false" border="false" autoScroll="true"
                                anchor="100%" bodyStyle="padding:20px">
                                <items>
                                    <mvc:ExtItem xtype="form" layout="form" id="frmAccount" labelWidth="130" defaultType="textfield"
                                        frame="false" border="false" autoScroll="true">
                                        <defaults>
                                            <mvc:ConfigItem name="width" value="100%" />
                                        </defaults>
                                        <items>
                                            <mvc:ExtItem fieldLabel="Old Password" id="OldPassword" name="OldPassword" allowBlank="true" inputType="password">
                                            </mvc:ExtItem>
                                            <mvc:ExtItem fieldLabel="New Password" id="NewPassword" name="NewPassword" allowBlank="true"
                                                inputType="password">
                                            </mvc:ExtItem>                                           
                                        </items>
                                    </mvc:ExtItem>
                                </items>
                            </mvc:ExtItem>                            
                            <mvc:ExtItem xtype="panel" title="Team" frame="false" border="false" autoScroll="true"
                                anchor="100%" bodyStyle="padding:20px">      
                                <listeners>
                                    <script>
                                        activate: function() { Ext.get('ExtGridPanel1').dom.style.display = 'block'; }
                                    </script>
                                </listeners>                          
                                <items>
                                    <mvc:ExtItem xtype="form" layout="form" id="ExtJSItem1" labelWidth="130" defaultType="textfield"
                                        frame="false" border="false" autoScroll="true">
                                        <defaults>
                                            <mvc:ConfigItem name="width" value="100%" />
                                        </defaults>
                                        <html>
                                        <mvc:ExtGridPanel ID="ExtGridPanel1" renderTo="ExtGridPanel1" Width="545" Height="250"
                                            autoHeight="true" loadMask="true" runat="server">                                            
                                            <store>
                                                <mvc:ExtStore autoLoad="true">                                                    
                                                    <sortInfo field="Name" direction="DESC" />
                                                    <proxy>
                                                        <mvc:ExtHttpProxy url="/User/GetAll" />
                                                    </proxy>
                                                    <reader>
                                                        <mvc:ExtJsonReader id="UserID" root="users">
                                                            <fields>
                                                                <mvc:ExtRecord name="UserID" />
                                                                <mvc:ExtRecord name="Role" />
                                                                <mvc:ExtRecord name="Name" />
                                                                <mvc:ExtRecord name="Email" />
                                                                <mvc:ExtRecord name="Mobile" />
                                                                <mvc:ExtRecord name="Telephone" />
                                                            </fields>
                                                        </mvc:ExtJsonReader>
                                                    </reader>
                                                </mvc:ExtStore>
                                            </store>
                                            <cm>
                                                <mvc:ExtColumn header="UserName" dataIndex="Name" />
                                                <mvc:ExtColumn header="Email" width="150" dataIndex="Email" />
                                                <mvc:ExtColumn header="Mobile" dataIndex="Mobile" />
                                                <mvc:ExtColumn header="Telephone" dataIndex="Telephone" />
                                                <mvc:ExtColumn header="Role" dataIndex="Role" />
                                            </cm>
                                            <selModel>
                                                <mvc:ExtRowSelectionModel singleSelect="true" />
                                            </selModel>
                                        </mvc:ExtGridPanel>
                                        </html>
                                        <defaults>
                                            <mvc:ConfigItem name="width" value="100%" />
                                        </defaults>
                                        <items>
                                            <mvc:ExtItem fieldLabel="Name" name="Name" allowBlank="false">
                                            </mvc:ExtItem>
                                            <mvc:ExtItem fieldLabel="Email" vtype="email" vtypeText="Please Enter a Valid Email Address."
                                                name="Email" allowBlank="false">
                                            </mvc:ExtItem>
                                            <mvc:ExtItem fieldLabel="Mobile" name="Mobile" allowBlank="true">
                                            </mvc:ExtItem>
                                            <mvc:ExtItem fieldLabel="Telephone" name="Telephone" allowBlank="true">                                                
                                            </mvc:ExtItem>
                                            <mvc:ExtItem id="Role" name="Role" xtype="combo" hiddenName="RoleID" valueField="value" triggerAction="All" fieldLabel="Role" displayField="text"
                                            mode="Local">                                                                                                                   
                                                <simpleStore>
                                                    <fields>
                                                        <mvc:ExtRecord name="value" />
                                                        <mvc:ExtRecord name="text" />
                                                    </fields>
                                                    <data>
                                                        [['1', 'Manager'],['2', 'Lead Developer'],['3', 'Developer']]
                                                    </data>
                                                </simpleStore>
                                            </mvc:ExtItem>                  
                                            <mvc:ExtItem xtype="panel" border="false" frame="false" name="telephone">
                                                <buttons>
                                                    <mvc:ExtItem xtype="button" text="Add" name="telephone" allowBlank="false">
                                                        <handler>

                                                            <script>
                                                                function AddRecord(btn) {

                                                                    var UserForm = getSetCmps().frmTeam.getForm();
                                                                    if (UserForm.isValid()) {
                                                                        btn.disabled = true;
                                                                        Ext.MessageBox.show({
                                                                            msg: 'Adding user, please wait',
                                                                            progressText: 'Added user is',
                                                                            width: 300,
                                                                            wait: true,
                                                                            waitConfig: {
                                                                                interval: 200
                                                                            }
                                                                        });
                                                                        var formvalue = UserForm.getValues();

                                                                        Ext.Ajax.request({
                                                                            url: '../User/AddUser',
                                                                            method: 'POST',
                                                                            params: { User: Ext.util.JSON.encode(formvalue) },
                                                                            callback: function(options, success, response) {
                                                                                if (success) {
                                                                                    Ext.MessageBox.hide();
                                                                                    var result = Ext.util.JSON.decode(response.responseText)
                                                                                    //Ext.MessageBox.alert("Message", result.Message);
                                                                                } else {
                                                                                    Ext.MessageBox.hide();
                                                                                    Ext.MessageBox.alert("Failure, please try again", response.responseText);
                                                                                }
                                                                            },
                                                                            // the function to be called upon failure of the request
                                                                            // (server script, 404, or 403 errors)
                                                                            failure: function(response, options) {
                                                                                Ext.MessageBox.hide();
                                                                                ReturnValue = Ext.MessageBox.alert("Warning, There was an unexpected error! Please contact administrator！");
                                                                            },
                                                                            success: function(response, options) {
                                                                                Ext.MessageBox.hide();
                                                                                getSetCmps().grdUsers.store.reload();
                                                                            }
                                                                        })// end Ajax request

                                                                    }
                                                                }
                                                            </script>

                                                        </handler>
                                                    </mvc:ExtItem>
                                                    <mvc:ExtItem xtype="button" text="Remove" name="telephone" allowBlank="false">
                                                        <handler>

                                                            <script>
                                                                function handleDelete() {

                                                                    var selectedKeys = getSetCmps().grdUsers.selModel.selections.keys; // returns
                                                                    // array of
                                                                    // selected
                                                                    // rows ids
                                                                    // only
                                                                    if (selectedKeys.length > 0) {
                                                                        Ext.MessageBox.confirm('Message', 'Are you sure you want to delete the record you selected？', deleteRecord);
                                                                    } else {
                                                                        Ext.MessageBox.alert('Message', 'Please select the record！');
                                                                    } // end
                                                                }                                                           
                                                            </script>

                                                        </handler>
                                                    </mvc:ExtItem>
                                                </buttons>
                                            </mvc:ExtItem>
                                        </items>
                                    </mvc:ExtItem>
                                </items>
                            </mvc:ExtItem>
                        </items>
                    </mvc:ExtItem>
                </items>
                <buttons>
                    <mvc:ExtItem text="Save">
                        <handler>

                            <script>
                                function() {
                                    //var params = { method: "saveUserAccountSettings" };
                                    var frmAccData = Ext.encode(getCmps().frmAccount.getForm().getValues());
                                    Ext.Ajax.request({
                                        url: '../User/ChangePassword',
                                        params: Ext.decode(frmAccData),
                                        success: (function(response, options) {
                                            settingsWin.hide();
                                            Ext.MessageBox.hide();
                                            var response = Ext.decode(response.responseText);
                                            var cmps = getCmps();
                                            if (!response.Result) {
                                                Ext.Msg.alert("Update failed", response.Message);
                                            }
                                            else {
                                                getCmps().frmop.setValue("");
                                                getCmps().frmnp.setValue("");
                                                getCmps().frmAccount.getForm().clearInvalid();
                                            }
                                        }),
                                        failure: (function(response) { Ext.MessageBox.hide(); Ext.Msg.alert("Communication error", response.responseText); })
                                    });
                                }
                            </script>

                        </handler>
                    </mvc:ExtItem>
                    <mvc:ExtItem text="Cancel">
                        <handler>

                            <script>
                                function() { settingsWin.hide(); }
                            </script>

                        </handler>
                    </mvc:ExtItem>
                </buttons>
                <listeners>

                    <script>
                        "show": function(cmp){
			                    }
                    </script>

                </listeners>
            </mvc:ExtItem>
        </xtype>

        <script>
            Ext.namespace("dlr.cmp.workSpace");
            dlr.cmp.workSpace.settingsWindow = function(cfg) {
                var that = this;
                var windowCfg = settingCfgs;
                var ldMsk = null;

                // ------------------------------------------------
                // public properties
                // ------------------------------------------------
                // ------------------------------------------------
                // public methods
                // ------------------------------------------------
                // ------------------------------------------------
                // constructor code
                // ------------------------------------------------
                Ext.apply(this, cfg);
                // apply private cfgs
                Ext.apply(this, windowCfg);
                dlr.cmp.workSpace.settingsWindow.superclass.constructor.call(this, cfg);
                // events
            };
            Ext.extend(dlr.cmp.workSpace.settingsWindow, Ext.Window);
            Ext.reg("settingsWindow", dlr.cmp.workSpace.settingsWindow);
        </script>
 
    </mvc:ExtJSON>
